package view
{
    import flash.display.*;

    public class Boss extends Sprite
    {
        private static const SPEED:Number = 0.05;

        public static const MOVE_NORMAL:int = 1;
        public static const MOVE_DOWN:int = 2;

        public static const FIRE_NORMAL:int = 1;

        public var healthPoint:int;
        public var attackPoint:int;
        public var fireMode:int;
        public var moveMode:int;
        public var gunX:Number;
        public var gunY:Number;

        private var _hit:Boolean;

        public function Boss(hp:Number, ap:Number)
        {
            healthPoint = hp;
            attackPoint = ap;
            _hit = false;
            fireMode = FIRE_NORMAL;
            moveMode = MOVE_DOWN;
            gunX = 0;
            gunY = 40;
        }

        public function reset(hp:Number, ap:Number):void
        {
            healthPoint = hp;
            attackPoint = ap;
            _hit = false;
            fireMode = FIRE_NORMAL;
            moveMode = MOVE_DOWN;
            gunX = 0;
            gunY = 40;
        }

        public function get hit():Boolean
        {
            return _hit;
        }

        public function set hit(v:Boolean):void
        {
            if(_hit != v)
            {
                _hit = v;
                if(_hit)
                {
                    display(0xFFFFFF);
                }
                else
                {
                    display();
                }
            }
        }

        public function display(c:uint=0xFF0000):void
        {
            graphics.clear();
            graphics.lineStyle(NaN);
            graphics.beginFill(0x00C000);
            graphics.drawCircle(0, 0, 50);
            graphics.endFill();

            graphics.lineStyle(15, 0xFFFF00);
            graphics.beginFill(c);
            graphics.moveTo(0, 0);
            graphics.lineTo(gunX, gunY);
            graphics.endFill();

            graphics.lineStyle(NaN);
            graphics.beginFill(c);
            graphics.drawCircle(0, 0, 20);
            graphics.endFill();
        }

        public function update(t:Number, toX:Number, toY:Number):void
        {
            if((moveMode & MOVE_NORMAL) == MOVE_NORMAL)
            {
                var dx:Number = toX - x;
                var dy:Number = toY - y;
                var l:Number = Math.sqrt((dx*dx) + (dy*dy));
                if(l < t * SPEED)
                {
                    x = toX;
                    y = toY;
                }
                else
                {
                    x += (t * SPEED) * dx / l;
                    y += (t * SPEED) * dy / l;
                }
            }
            else
            {
                y += t * SPEED;
            }
            gunX = 40 * dx / l;
            gunY = 40 * dy / l;
            if(_hit)
            {
                display(0xFFFFFF);
            }
            else
            {
                display();
            }
        }
    }
}
